<template>
  <el-dropdown
    trigger="click"
    class="international"
    @command="handleSetLanguage"
    id="guide-lang"
  >
    <div>
      <el-tooltip :effect="effect" :content="$t('msg.navBar.lang')">
        <div>
          <svg-icon icon="language" />
        </div>
      </el-tooltip>
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item :disabled="language == 'zh'" command="zh"
          >中文</el-dropdown-item
        >
        <el-dropdown-item :disabled="language == 'en'" command="en"
          >English</el-dropdown-item
        >
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup>
import { computed, defineProps } from 'vue'
import { useStore } from 'vuex'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'
defineProps({
  effect: {
    type: String,
    default: 'dark',
    validator: function (value) {
      return ['dark', 'light'].indexOf(value) != -1
    }
  }
})
const store = useStore()
// 获取现在的language
const language = computed(() => store.getters.language)
const i18n = useI18n()
// 点击事件切换language
const handleSetLanguage = (language) => {
  i18n.locale.value = language
  store.commit('app/setLanguage', language)
  ElMessage.success(i18n.t('msg.toast.switchLangSuccess'))
}
</script>

<style lang="scss" scoped></style>
